<template>
  <el-form
    ref="form"
    :label-position="device === 'mobile' ? 'top' : 'right'"
    label-width="120px"
    :model="form"
    :rules="rules"
  >
    <el-form-item label="项目名称" prop="name">
      <el-input
        v-model.trim="form.name"
        autocomplete="off"
        placeholder="请输入项目名称"
      />
    </el-form-item>
    <el-form-item label="金额(万元)" prop="price">
      <el-input
        v-model.trim="form.price"
        autocomplete="off"
        placeholder="请输入金额(万元)"
        type="number"
      />
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
        v-model="form.startTime"
        placeholder="开始时间"
        style="width: 100%"
        value-format="timestamp"
      />
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
        v-model="form.endTime"
        placeholder="请选择验收日期"
        style="width: 100%"
        value-format="timestamp"
      />
    </el-form-item>
    <el-form-item label="施工人员/单位" prop="acceptanceWorker">
      <el-input
        v-model.trim="form.acceptanceWorker"
        autocomplete="off"
        placeholder="请输入施工人员/单位"
      />
    </el-form-item>
    <el-form-item label="验收日期" prop="acceptanceTime">
      <el-date-picker
        v-model="form.acceptanceTime"
        placeholder="请选择验收日期"
        style="width: 100%"
        value-format="timestamp"
      />
    </el-form-item>
    <el-form-item label="验收人员" prop="worker">
      <el-input
        v-model.trim="form.worker"
        autocomplete="off"
        placeholder="请输入验收人员"
      />
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input
        v-model.trim="form.remark"
        autocomplete="off"
        placeholder="请输入备注"
        :rows="2"
        type="textarea"
      />
    </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex";
import { API_repairCreate, API_repairEdit } from '@/api/repair'
export default {
  name: "FormEdit",
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        name: "",
        price: "",
        acceptanceWorker: "",
        acceptanceTime: "",
        startTime:"",
        endTime:"",
        worker: "",
        remark: "",
      },
      rules: {
        name: [{ required: true, trigger: "blur", message: "请输入项目名称" }],
        price: [
          { required: true, trigger: "blur", message: "请输入金额(万元)" },
        ],
        startTime: [
          { required: true, trigger: "blur", message: "请输入开始时间" },
        ],
        endTime: [
          { required: true, trigger: "blur", message: "请输入结束时间" },
        ],
        acceptanceWorker: [
          { required: true, trigger: "blur", message: "请输入施工人员/单位" },
        ],
        acceptanceTime: [
          { required: true, trigger: "blur", message: "请输入验收日期" },
        ],
        worker: [
          { required: true, trigger: "blur", message: "请输入验收人员" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  watch: {
    formData: {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData);
        }
      },
      immediate: true,
    },
  },
  methods: {
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      if (this.device === "mobile") {
        this.$router.push({
          name: "Repair",
          params: { refresh: true },
        });
      }
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.form.startTime > this.form.endTime) {
            return this.$baseMessage('开始时间不能大于结束时间', 'error')
          }
          if (this.form.code) {
            const { message } = await API_repairEdit(this.form);
            this.$baseMessage(message, "success");
          } else {
            const { message } = await API_repairCreate(this.form);
            this.$baseMessage(message, "success");
          }
          this.$emit("fetch-data");
          this.close();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
